﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Services.Interface.VehicleDispatch;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace RndMaterialDisposalService.Controllers.VehicleDispatch
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CarManagementController : ControllerBase
    {
        private readonly ILogger<CarManagementController> logger;
        private readonly ICarConfigurationService carConfigurationService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="carConfigurationService"></param>
        public CarManagementController(ILogger<CarManagementController> logger, ICarConfigurationService carConfigurationService)
        {
            this.logger = logger;
            this.carConfigurationService = carConfigurationService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="carManageReq"></param>
        /// <returns></returns>
        [HttpPost("GetCarConfigList")]
        [PageList]
        public async Task<IActionResult> GetCarConfigList(CarManagementReq carManageReq)
        {
            var result = await carConfigurationService.GetCarConfigurationList(carManageReq);
            var response = ApiResponsePageList<CarConfigurationDto>.CreatePageListByResult(result.list, carManageReq.PageIndex, carManageReq.PageSize, result.count);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("GetCarListByKeyword")]
        [PageList]
        public async Task<IActionResult> GetCarListByKeyword(string? keyword, int PageIndex = 1, int PageSize = 10)
        {
            var result = await carConfigurationService.GetCarListByKeyword(keyword ?? "", PageIndex, PageSize);
            var response = ApiResponsePageList<CarConfigurationDto>.CreatePageListByResult(result.list, PageIndex, PageSize, result.count);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AddCarConfig")]
        public async Task<IActionResult> AddCarConfig(CarConfigurationDto model)
        {
            model.CreateDate = DateTime.Now;
            var result = await carConfigurationService.AddCarConfiguration(model);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("UpdateCarConfig")]
        public async Task<IActionResult> UpdateCarConfig(CarConfigurationDto model)
        {
            var result = await carConfigurationService.UpdateCarConfiguration(model);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysId"></param>
        /// <returns></returns>
        [HttpPost("DeleteCarConfig")]
        public async Task<IActionResult> DeleteCarConfig(Guid sysId)
        {
            logger.LogInformation($"Delete CarConfig : {sysId}");
            var result = await carConfigurationService.DeleteCarConfiguration(sysId);
            return Ok(result);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="driverName"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpPost("GetDriverConfigList")]
        [PageList]
        public async Task<IActionResult> GetDriverConfigList(string? driverName, int PageIndex = 1, int PageSize = 10)
        {
            var result = await carConfigurationService.GetDriverConfigurationList(driverName ?? "", PageIndex, PageSize);
            var response = ApiResponsePageList<DriverConfigurationDto>.CreatePageListByResult(result.list, PageIndex, PageSize, result.count);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet("GetDriverListByKeyword")]
        [PageList]
        public async Task<IActionResult> GetDriverListByKeyword(string? keyword, int PageIndex = 1, int PageSize = 10)
        {
            var result = await carConfigurationService.GetDriverListByKeyword(keyword ?? "", PageIndex, PageSize);
            var response = ApiResponsePageList<DriverConfigurationDto>.CreatePageListByResult(result.list, PageIndex, PageSize, result.count);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("AddDriverConfig")]
        public async Task<IActionResult> AddDriverConfig(DriverConfigurationDto model)
        {
            model.CreateDate = DateTime.Now;
            var result = await carConfigurationService.AddDriverConfiguration(model);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("UpdateDriverConfig")]
        public async Task<IActionResult> UpdateDriverConfig(DriverConfigurationDto model)
        {
            var result = await carConfigurationService.UpdateDriverConfiguration(model);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysId"></param>
        /// <returns></returns>
        [HttpPost("DeleteDriverConfig")]
        public async Task<IActionResult> DeleteDriverConfig(Guid sysId)
        {
            logger.LogInformation($"Delete DriverConfig : {sysId}");
            var result = await carConfigurationService.DeleteDriverConfiguration(sysId);
            return Ok(result);
        }

    }
}
